import { fileURLToPath, URL } from 'node:url'

import { defineConfig,loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'

// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
  const env = loadEnv(mode, process.cwd())
  //console.log(env)
  return {
    plugins: [
      vue(),
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    },
    server: {
      proxy: {
        '/api': {
          target: env.VITE_BASE_URL,
          changeOrigin: true,
          rewrite: path => {
            //console.log(path)  //axios请求地址
            //console.log(path.replace(/^\/api/, ""))
            return path.replace(/^\/api/, "");
            /* /api/city  target+rewrite函数的返回值拼串 */
          }
        },
        '/map': {
          target: env.VITE_BASE_GAODE,
          changeOrigin: true,
          rewrite: path =>{
            //console.log(path)
            //console.log(path.replace(/^\/map/, ""))
            return path.replace(/^\/map/, "")
          }
        }
      }
    }
  }
})